package cn.fastoo.web.backstage.web.controller.product;

import cn.fastoo.common.dao.product.ProductSkuDao;
import cn.fastoo.common.entity.product.ProductSku;
import cn.fastoo.common.sampleframe.SFBeanUtils;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.common.sampleframe.search.QuerySearch;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.common.sampleframe.springside.Collections3;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by lowkey on 17/10/11.
 */
@Controller
@RequestMapping("/product/sku")
public class ProductSkuController {

    @Autowired
    private ProductSkuDao dao;


    @RequestMapping
    public String index() {
        return "product/sku/index";
    }


    @RequestMapping("/manager")
    @ResponseBody
    public String manager(@ModelAttribute ProductSku newData, QuerySearch querySearch, String op) {
        Search search = querySearch.getSearch();
        if(StringUtils.isNotBlank(op)){
            if("add".equals(op)){
                dao.insert(newData);
                //logger.info(loginUser, SystemLog.TYPE_AUTH_ROLE_ADD,String.format("用户角色[%s]",authRole.getRoleName()));
            }else {
                ProductSku oldData = dao.findById(newData.getId());
                if(oldData==null){
                    return JsonResult.success("错误参数,请检查");
                }
                if("save".equals(op)){
                    newData.setVersion(oldData.getVersion());
                    dao.update(newData);
                }else if("del".equals(op)){
                    dao.delete(oldData);
                }
            }
            return JsonResult.success("操作成功");
        }
        return JsonResult.querySearchSuccess(dao.find(search),querySearch);
    }

    @RequestMapping("/batchAddSku")
    @ResponseBody
    public String batchAddSku(@RequestParam String json,@RequestParam Long productId) {
        JSONArray array = JSON.parseArray(json);
        List<ProductSku> productSkus = new ArrayList<ProductSku>();

        for (int i = 0; i <array.size() ; i++) {

            JSONObject attr = array.getJSONObject(i);
            JSONObject values = attr.getJSONObject("values");
            if(Collections3.isEmpty(productSkus)){
                for(String valueId:values.keySet()){
                    ProductSku ps = new ProductSku();
                    ps.setProductId(productId);
                    ps.setCode(attr.getString("id")+":"+valueId+";");
                    ps.setCodeName(attr.getString("name")+":"+values.getString(valueId)+";");
                    productSkus.add(ps);
                }
            }else{
                List<ProductSku> newSkus = new ArrayList<ProductSku>();
                for(ProductSku oldPs : productSkus){
                    for(String valueId:values.keySet()){
                        ProductSku ps = SFBeanUtils.injectByObject(new ProductSku(),oldPs);
                        ps.setCode(ps.getCode()+attr.getString("id")+":"+valueId+";");
                        ps.setCodeName(ps.getCodeName()+attr.getString("name")+":"+values.getString(valueId)+";");
                        newSkus.add(ps);
                    }
                }
                productSkus = newSkus;
            }
        }
        dao.batchInsert(productSkus);
      return JsonResult.success("ok");
    };


}
